/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.dummydevice;

import net.zarubsys.robotplayground.core.session.DeviceSessionContainer;
import net.zarubsys.robotplayground.core.session.Session;
import net.zarubsys.robotplayground.dummydevice.DummyDeviceConfiguration;
import net.zarubsys.robotplayground.gui.dummydevice.plugin.DummyDeviceGUIPlugin;
import net.zarubsys.robotplayground.gui.session.providers.ISystemObjectGUIProvider;
import net.zarubsys.robotplayground.gui.session.providers.SystemObjectGUIProviderClass;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;

/**
 * DummyDeviceGUIProvider
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class DummyDeviceGUIProvider implements ISystemObjectGUIProvider {

	@Override
	public Image getImage(String id) {
		return DummyDeviceGUIPlugin.getDefault().getImage(DummyDeviceGUIPlugin.DUMMY_DEVICE);
	}

	@Override
	public String getDescription(String id) {
		return "Dummy Device";
	}

	@Override
	public SystemObjectGUIProviderClass getProviderClass() {
		return SystemObjectGUIProviderClass.DEVICE;
	}

	@Override
	public int addObjectToSession(Session session, String id) {
		DummyDeviceSettingsDialog dialog = new DummyDeviceSettingsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
		dialog.create();
		if (dialog.open() != Window.OK) return -1;
		
		session.getDevices().add(new DeviceSessionContainer(id, new DummyDeviceConfiguration(dialog.getX(), dialog.getY(), dialog.getDirection())));
		return session.getDevices().size() - 1;
	}

	@Override
	public boolean reconfigure(Session session, int index) {
		DeviceSessionContainer container = session.getDevices().get(index);
		Assert.isLegal(container.getConfiguration() instanceof DummyDeviceConfiguration, "Expecting instance of DummyDeviceConfiguration");
		DummyDeviceConfiguration currentCfg = (DummyDeviceConfiguration) container.getConfiguration();
		
		DummyDeviceSettingsDialog dialog = new DummyDeviceSettingsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
		dialog.setX(currentCfg.getInitialX());
		dialog.setY(currentCfg.getInitialY());
		dialog.setDirection(currentCfg.getDirection());
		dialog.create();
		
		if (dialog.open() != Window.OK) return false;
		container.setConfiguration(new DummyDeviceConfiguration(dialog.getX(), dialog.getY(), dialog.getDirection()));
		return true;
	}

	@Override
	public boolean isConfigurable(String id) {
		return true;
	}

	@Override
	public boolean remove(Session session, int index) {
		session.getDevices().remove(index);
		return true;
	}

	@Override
	public String getDescription(Session session, int index) {
		return getDescription(session.getDevices().get(index).getDeviceId());
	}

	@Override
	public String validate(Session session, int index) {
		// nothing to validate
		return null;
	}

}

